package u.hnx.admin.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import u.hnx.admin.security.api.LoginVerify;
import u.hnx.admin.security.utils.ResponseUtil;

import java.io.IOException;
@Component
public record AuthenticationFailureImpl(ResponseUtil responseUtil, LoginVerify loginVerify) implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        if(exception instanceof AuthenticationException && exception.getMessage().indexOf("用户名") >= 0){
            String username = loginVerify.getRetryKey(request);
            loginVerify.setRetryCount(username,loginVerify.getRetryCount(username));
        }
        responseUtil.printFail(request,response,exception.getMessage());
    }
}
